#! /usr/bin/env python
#coding=utf-8
from __future__ import with_statement

from uliweb import expose, decorators

def get_duties_list_view(c):
    from uliweb.utils.generic import ListView
    from uliweb.orm import get_model
    from uliweb import request
    from uliweb.core.html import Tag
    from uliweb import orm
    
    def name(value, obj):
        return str(Tag('a', value, href='/resource/duties/%d' % obj.id))
        
    pageno = int(request.GET.get('pageno', 1)) - 1
    
    Duties = get_model('duties')
    #query = None
#    query = Team.filter
#    if request.user.is_manager:
#        query = request.user.members.all()
    condition = None
#    if c.get('tx'):
#        condition = (team.c.tx == int(c['tx'])) & condition
#    if c.get('kfzx'):
#        condition = (Team.c.kfzx == int(c['kfzx'])) & condition
    if c.get('name'):
        condition = (Duties.c.name.like(c['name'])) & condition
#    if c.get('department'):
#        condition = (team.c.department == c['department']) & condition
#    query = Team.filter()
    fields_convert_map = {'name':name}
    view =  ListView(Duties, condition=condition, 
        rows_per_page=settings.get_var('PARA/ROWS_PER_PAGE', 10), pageno=pageno, 
        fields_convert_map=fields_convert_map, id='teams_table')
#    view.types_convert_map = {orm.BooleanProperty:boolean_convert}
    return view

@decorators.require_login
@expose('/resource/duties/list')
def resource_duties_list():
    from query import create_duties_query

    query_view = create_duties_query(url_for(resource_duties_list))
    c = query_view.run()

    view = get_duties_list_view(c)
    if 'data' in request.GET:
        result = view.run(head=False, body=True)
        return json(result)
    else:
        result = view.run(head=True, body=False)
        result.update({'query_form':query_view.form})
        return result
    
@expose('/resource/duties/data')
def resource_duties_data():
    result = view.run(head=False, body=True)#此处的view从何而来？
    return json(result)

@decorators.require_login
@expose('/resource/duties/add')
def resource_duties_add():
    from uliweb.utils.generic import AddView, ManyToManySelectField
    from uliweb.orm import get_model
#    from forms import UserForm, EditUserForm
    import generic.views as g_views
    from functools import partial
    
    get_url = g_views.get_url('/resource/duties', restful=True)
    
    Duties = get_model('duties')
    
#    def pre_save(data):
#        if request.user.is_manager:
#            data['kfzx'] = request.user._kfzx_
#            data['tx'] = request.user._tx_
#            data['department'] = request.user.department
    
#    def post_save(obj, data):
#        obj.set_password(settings.PARA.DEFAULT_PASSWORD)
#        obj.users_systems.update(*data['system'])
#        obj.observers_systems.update(*data['ob_system'])
        
    if request.user.is_superuser:
        fields = ('name',)
#        layout = ('abbr',  'name', 'desc', 'logo')

        view = AddView(Duties, url_for(resource_duties_list), fields=fields)#, pre_save=pre_save)

    else:
        flash('你不是超级管理员，无法增加职务信息！')
        return redirect(url_for(resource_duties_list))
    return view.run()
    
@decorators.require_login
@expose('/resource/duties/<int:id>')
def resource_duties_view(id):
    from uliweb.utils.generic import DetailView
#    from uliweb import orm
    from uliweb.orm import get_model
    
    Duties = get_model('duties')
 
    fields = ('name',)
    view = DetailView(Duties, int(id), fields=fields)
    return view.run()
    
@decorators.require_login
@expose('/resource/duties/<int:id>/edit')
def resource_duties_edit(id):
    from uliweb.utils.generic import EditView, ManyToManySelectField
    from uliweb.orm import get_model, get_connection
#    from forms import EditUserForm, UserForm

    Duties = get_model('duties')
    duties = Duties.get(id)
    
    
    if request.user.is_superuser:
        fields = ('name',)
        view = EditView(Duties, condition=int(id), ok_url=url_for(resource_duties_view, id=id), fields=fields)
        return view.run()

    else:
        flash('你不是超级用户，无法编辑此职务信息！')
        return redirect(url_for(resource_duties_list))
    
@decorators.require_login
@expose('/resource/duties/<int:id>/delete')
def resource_duties_delete(id):
    from uliweb.utils.generic import DeleteView
    from uliweb.orm import get_model
    Duties = get_model('duties')
    duties = Duties.get(id)
    
    if request.user.is_superuser:
        view = DeleteView(Duties, condition=int(id), ok_url=url_for(resource_duties_list))
        return view.run()
    else:
        flash('你不是超级用户，无法删除职务信息！')
        return redirect(url_for(resource_duties_view, id=int(id)))
    

        
